package com.yqg.education.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fasterxml.jackson.annotation.JsonFormat;
import com.yqg.education.entity.ExamLog;
import com.yqg.education.mapper.ExamLogMapper;
import com.yqg.education.service.IExamLogService;
import com.yqg.education.service.IUserService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * ExamLogServiceImpl 服务实现类
 * @author 张芸博
 * @since 2025-04-07
 */
@Service
public class ExamLogServiceImpl extends ServiceImpl<ExamLogMapper, ExamLog> implements IExamLogService {
    @Resource
    private ExamLogMapper examLogMapper;
    @Resource
    private IUserService userService;

    @Override
    public Map<String, Object> findExamPage(Integer pageNum, Integer pageSize, String name, Integer typeId, Integer userId, Integer flow) {
        Integer pageStartNumber = pageNum * pageSize - pageSize;
        Map<String,Object> map = new HashMap<>();
        //获取学员对应所在班级的课程ID
        List<Long> courseId =  userService.getCourseIdByUserId(userId);
        List<Map<String,Object>> examList = new ArrayList<>();
        Integer count = 0;
        //判断查询
        if(flow == null || flow == 0){
//            全部考试记录 和 考试未完成记录
            examList = examLogMapper.findAllAndCompletedExamPage(pageStartNumber,pageSize,name,typeId,courseId,userId,flow);
            examList.forEach(examLog -> {
                // 时间格式调整
                LocalDateTime createTime = (LocalDateTime) examLog.get("createTime");
                examLog.put("createTime", createTime.atZone(ZoneId.systemDefault())
                        .withZoneSameInstant(ZoneId.of("Asia/Shanghai"))
                        .format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));

                //  标记flow状态
                if (examLog.get("flow") == null || !examLog.get("flow").equals(1)) {
                    examLog.put("flow",0);
                }else {
                    examLog.put("flow",1);
                }
                //  标记isPass状态
                if (examLog.get("isPass") == null || !examLog.get("isPass").equals(1)) {
                    examLog.put("isPass",0);
                }else {
                    examLog.put("isPass",1);
                }
            });
            count = examLogMapper.findAllAndCompletedExamPageCount(name,typeId,courseId,userId,flow);
        }
        else if(flow == 1){
//            考试已完成记录查询
            examList = examLogMapper.findIncompleteExamPage(pageStartNumber,pageSize,name,typeId,courseId,userId,flow);
            examList.forEach(examLog -> {
                // 时间格式调整
                LocalDateTime createTime = (LocalDateTime) examLog.get("createTime");
                examLog.put("createTime", createTime.atZone(ZoneId.systemDefault())
                        .withZoneSameInstant(ZoneId.of("Asia/Shanghai"))
                        .format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));

                //  标记flow状态
                if (examLog.get("flow") == null || !examLog.get("flow").equals(1)) {
                    examLog.put("flow",0);
                }else {
                    examLog.put("flow",1);
                }
                //  标记isPass状态
                if (examLog.get("isPass") == null || !examLog.get("isPass").equals(1)) {
                    examLog.put("isPass",0);
                }else {
                    examLog.put("isPass",1);
                }
            });
            count = examLogMapper.findIncompleteExamPageCount(name,typeId,courseId,userId,flow);
        }
        map.put("records", examList);
        map.put("total", count);
        return map;
    }

    @Override
    public ExamLog findExamScore(Integer userId, Integer courseId) {
        ExamLog examScore = examLogMapper.findExamScore(userId, courseId);
        long exampaperId = examScore.getExampaperId();
        examScore.setCorrectQuantityNum(examLogMapper.findExamAccuracy(userId,(int) exampaperId)) ;
        if (examScore!=null){
            Integer rank = examLogMapper.getRank(examScore.getExampaperGetScore(), userId, courseId);
            examScore.setRank(rank);
        }
        return examScore;
    }
}
